package com.scalar.chiptrack.operations.reports.actions;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.reports.forms.YieldGraphForm;
import com.scalar.chiptrack.users.User;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: Sep 21, 2005
 * Time: 1:50:46 PM
 * To change this template use Options | File Templates.
 */
public class YieldGraphReportAction extends ChipTrackAction
{
    private static final String ACTION_NAME = "/yieldGraphReport.do";

    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response )
            throws Exception
    {
        HttpSession session = request.getSession();

        User user = ( User ) session.getAttribute( SessionKeys.USER_KEY );
        if ( user == null )
        {
            session.setAttribute( SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString() );
            return ( mapping.findForward( "login" ) );
        }

        YieldGraphForm yieldGraphForm = (YieldGraphForm) form;
        HashMap errors = new HashMap();

        String processType = yieldGraphForm.getProcessType();
        String baseProductIdStr = yieldGraphForm.getBaseProductId();
        int baseProductIdInt = -1;
        ArrayList baseProductList   = null;
        ArrayList partNumberList    = null;

        try
        {
            baseProductList = ProductDao.loadChipBaseProducts(null);
            if( processType != null && !("-1".equals(processType.trim())) )
            {
                try
                {
                    baseProductIdInt = Integer.parseInt( baseProductIdStr );
                }
                catch( NumberFormatException e)
                {
                    e.printStackTrace();
                }
                if( baseProductIdInt != -1)
                {
                    partNumberList = ProductDao.loadAllPartNumberByBaseProductIdAndProcessType( baseProductIdInt, processType);
                }
                else
                {
                    partNumberList = ProductDao.loadAllPartsForProcessType( processType );
                }
                request.setAttribute( "partNumberList", partNumberList );
                yieldGraphForm.setProcessType( processType );
                yieldGraphForm.setBaseProductId( baseProductIdStr );
            }
            else
            {
               yieldGraphForm.setBaseProductId( "-1" );
            }
        }
        catch( Exception e )
        {
            e.printStackTrace();
            errors.put(ERROR_INTERNAL_SERVER_ERROR, null);
        }
        request.setAttribute( "baseProductList", baseProductList );
        return mapping.findForward( "success" );
    }
 }

